package org.groovymud.shell.security.callbacks;

import org.groovymud.shell.io.TerminalIO;

import javax.security.auth.callback.ConfirmationCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import java.io.IOException;
import java.util.Map;

public class SelfHandlingConfirmationCallback extends ConfirmationCallback implements SelfHandlingCallback{

    public SelfHandlingConfirmationCallback(String message, int information, int yesNoOption, int defaultOption) {
        super(message, information, yesNoOption, defaultOption);
    }

    public void handle(TerminalIO terminalIO, Map<String, Object> callbackData) throws IOException, UnsupportedCallbackException {
        if (getMessageType() != ConfirmationCallback.YES_NO_OPTION) {
            throw new UnsupportedCallbackException(this, "cannot handle none YN questions yet");
        }
        terminalIO.writeln(getPrompt());
        terminalIO.flush();
        String response = terminalIO.readln(false);

        if (response.substring(0, 1).equalsIgnoreCase("y")) {
            setSelectedIndex(ConfirmationCallback.YES);
        } else {
            setSelectedIndex(ConfirmationCallback.NO);
        }
    }
}
